# -*- coding: utf-8 -*-
#!/usr/local/env    python

import os
import functools

get_metrics_with_local_methods = list()

def register_local_method(fn):
    @functools.wraps(fn)
    def decorator(*args, **kwargs):
        return fn(*args, **kwargs)
    get_metrics_with_local_methods.append(fn)
    return decorator

@register_local_method
def get_nginx_workers():
    # 检查nginx worker 数
    nginx_worker_num = os.popen("ps -ef|grep 'nginx: worker process' | grep -v grep | wc -l").read()
    return "nginx_worker_number " + nginx_worker_num

@register_local_method
def get_minikube_status():
    if os.system("which minikube") != 0:
        print("not install minikube")
        return "minikube_status 0\ncluster_status 0\n"
    status_str = os.popen('minikube status | grep -E "minikube:|cluster:"').read()
    status_lst = status_str.split("\n")
    minikube = status_lst[0].endswith("Running")
    cluster = status_lst[1].endswith("Running")
    minikube_status = "minikube_status: %d\n" % minikube
    cluster_status = "cluster_status: %d\n" % cluster
    return minikube_status + cluster_status
